/**
 * Framework for batch testing user profile models
 * Final project by Sergey Nepomnyachiy and Julia Polchin
 * Supervisor: Tsvi Kuflik
 *
 */
package core.generators.nested;

import interfaces.GeneratorInterface;

import java.util.Collection;
import java.util.Iterator;
import java.util.Vector;

/**
 * Layer 2 generator - joins nested gen's data 
 * @author Sergey and Julia
 * 
 */
public class JoinGenerator implements GeneratorInterface<Object> {
	private Collection<GeneratorInterface<?>> nested;
	private Vector<Object> vector = null;

	/**
	 * Constructor 
	 * @param nested Collection of nested gens.
	 */
	public JoinGenerator(Collection<GeneratorInterface<?>> nested) {
		this.nested = nested;
	}

	@Override
	public Object getScalar() {
		return null;
	}

	@Override
	/**
	 * Assumption: all vectors have same length
	 */
	public Collection<Object> getVector() {
		if (null == this.vector) {
			this.vector = new Vector<Object>();
			Vector<Iterator<?>> iterators = new Vector<Iterator<?>>(this.nested
					.size());
			for (GeneratorInterface<?> g : this.nested) {
				Collection<?> generated = NestedHelper.tryUnwrap(g.getVector());
					iterators.add(generated.iterator());
			}

			while (iterators.elementAt(0).hasNext()) {
				Vector<Object> row = new Vector<Object>();
				for (Iterator<?> i : iterators)
					row.add(i.next());

				this.vector.add(row);
			}
		}
		//System.out.println("Joined: " + this.vector);
		return this.vector;
	}

	@Override
	public boolean is2D() {
		return true;
	}

}
